home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / man / cat.1 / perlobj.1 < prev    next >
Text File  |  1995-07-25  |  15KB  |  397 lines

  1.  
  2.  
  3.  
  4.      PPPPEEEERRRRLLLLOOOOBBBBJJJJ((((1111))))  UUUUNNNNIIIIXXXX SSSSyyyysssstttteeeemmmm VVVV ((((RRRReeeelllleeeeaaaasssseeee 0000....0000 PPPPaaaattttcccchhhhlllleeeevvvveeeellll 00000000))))  PPPPEEEERRRRLLLLOOOOBBBBJJJJ((((1111))))
  5.  
  6.  
  7.  
  8.      NNNNAAAAMMMMEEEE
  9.           perlobj - Perl objects
  10.  
  11.      DDDDEEEESSSSCCCCRRRRIIIIPPPPTTTTIIIIOOOONNNN
  12.           First of all, you need to understand what references are in
  13.           Perl.  See the _p_e_r_l_r_e_f manpage for that.
  14.  
  15.           Here are three very simple definitions that you should find
  16.           reassuring.
  17.  
  18.           1.  An object is simply a reference that happens to know
  19.               which class it belongs to.
  20.  
  21.           2.  A class is simply a package that happens to provide
  22.               methods to deal with object references.
  23.  
  24.           3.  A method is simply a subroutine that expects an object
  25.               reference (or a package name, for static methods) as the
  26.               first argument.
  27.  
  28.           We'll cover these points now in more depth.
  29.  
  30.           AAAAnnnn OOOObbbbjjjjeeeecccctttt iiiissss SSSSiiiimmmmppppllllyyyy aaaa RRRReeeeffffeeeerrrreeeennnncccceeee
  31.  
  32.           Unlike say C++, Perl doesn't provide any special syntax for
  33.           constructors.  A constructor is merely a subroutine that
  34.           returns a reference that has been "blessed" into a class,
  35.           generally the class that the subroutine is defined in.  Here
  36.           is a typical constructor:
  37.  
  38.               package Critter;
  39.               sub new { bless {} }
  40.  
  41.           The {} constructs a reference to an anonymous hash
  42.           containing no key/value pairs.  The _b_l_e_s_s() takes that
  43.           reference and tells the object it references that it's now a
  44.           Critter, and returns the reference.  This is for
  45.           convenience, since the referenced object itself knows that
  46.           it has been blessed, and its reference to it could have been
  47.           returned directly, like this:
  48.  
  49.               sub new {
  50.                   my $self = {};
  51.                   bless $self;
  52.                   return $self;
  53.               }
  54.  
  55.           In fact, you often see such a thing in more complicated
  56.           constructors that wish to call methods in the class as part
  57.           of the construction:
  58.  
  59.  
  60.  
  61.  
  62.  
  63.      Page 1                                          (printed 6/30/95)
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.      PPPPEEEERRRRLLLLOOOOBBBBJJJJ((((1111))))  UUUUNNNNIIIIXXXX SSSSyyyysssstttteeeemmmm VVVV ((((RRRReeeelllleeeeaaaasssseeee 0000....0000 PPPPaaaattttcccchhhhlllleeeevvvveeeellll 00000000))))  PPPPEEEERRRRLLLLOOOOBBBBJJJJ((((1111))))
  71.  
  72.  
  73.  
  74.               sub new {
  75.                   my $self = {}
  76.                   bless $self;
  77.                   $self->initialize();
  78.                   $self;
  79.               }
  80.  
  81.           Within the class package, the methods will typically deal
  82.           with the reference as an ordinary reference.  Outside the
  83.           class package, the reference is generally treated as an
  84.           opaque value that may only be accessed through the class's
  85.           methods.
  86.  
  87.           A constructor may rebless a referenced object currently
  88.           belonging to another class, but then the new class is
  89.           responsible for all cleanup later.  The previous blessing is
  90.           forgotten, as an object may only belong to one class at a
  91.           time.  (Although of course it's free to inherit methods from
  92.           many classes.)
  93.  
  94.           A clarification:  Perl objects are blessed.  References are
  95.           not.  Objects know which package they belong to.  References
  96.           do not.  The _b_l_e_s_s() function simply uses the reference in
  97.           order to find the object.  Consider the following example:
  98.  
  99.               $a = {};
  100.               $b = $a;
  101.               bless $a, BLAH;
  102.               print "\$b is a ", ref($b), "\n";
  103.  
  104.           This reports $b as being a BLAH, so obviously _b_l_e_s_s()
  105.           operated on the object and not on the reference.
  106.  
  107.           AAAA CCCCllllaaaassssssss iiiissss SSSSiiiimmmmppppllllyyyy aaaa PPPPaaaacccckkkkaaaaggggeeee
  108.  
  109.           Unlike say C++, Perl doesn't provide any special syntax for
  110.           class definitions.  You just use a package as a class by
  111.           putting method definitions into the class.
  112.  
  113.           There is a special array within each package called @ISA
  114.           which says where else to look for a method if you can't find
  115.           it in the current package.  This is how Perl implements
  116.           inheritance.  Each element of the @ISA array is just the
  117.           name of another package that happens to be a class package.
  118.           The classes are searched (depth first) for missing methods
  119.           in the order that they occur in @ISA.  The classes
  120.           accessible through @ISA are known as base classes of the
  121.           current class.
  122.  
  123.           If a missing method is found in one of the base classes, it
  124.           is cached in the current class for efficiency.  Changing
  125.           @ISA or defining new subroutines invalidates the cache and
  126.  
  127.  
  128.  
  129.      Page 2                                          (printed 6/30/95)
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.      PPPPEEEERRRRLLLLOOOOBBBBJJJJ((((1111))))  UUUUNNNNIIIIXXXX SSSSyyyysssstttteeeemmmm VVVV ((((RRRReeeelllleeeeaaaasssseeee 0000....0000 PPPPaaaattttcccchhhhlllleeeevvvveeeellll 00000000))))  PPPPEEEERRRRLLLLOOOOBBBBJJJJ((((1111))))
  137.  
  138.  
  139.  
  140.           causes Perl to do the lookup again.
  141.  
  142.           If a method isn't found, but an AUTOLOAD routine is found,
  143.           then that is called on behalf of the missing method.
  144.  
  145.           If neither a method nor an AUTOLOAD routine is found in
  146.           @ISA, then one last try is made for the method (or an
  147.           AUTOLOAD routine) in a class called UNIVERSAL.  If that
  148.           doesn't work, Perl finally gives up and complains.
  149.  
  150.           Perl classes only do method inheritance.  Data inheritance
  151.           is left up to the class itself.  By and large, this is not a
  152.           problem in Perl, because most classes model the attributes
  153.           of their object using an anonymous hash, which serves as its
  154.           own little namespace to be carved up by the various classes
  155.           that might want to do something with the object.
  156.  
  157.           AAAA MMMMeeeetttthhhhoooodddd iiiissss SSSSiiiimmmmppppllllyyyy aaaa SSSSuuuubbbbrrrroooouuuuttttiiiinnnneeee
  158.  
  159.           Unlike say C++, Perl doesn't provide any special syntax for
  160.           method definition.  (It does provide a little syntax for
  161.           method invocation though.  More on that later.)  A method
  162.           expects its first argument to be the object or package it is
  163.           being invoked on.  There are just two types of methods,
  164.           which we'll call static and virtual, in honor of the two C++
  165.           method types they most closely resemble.
  166.  
  167.           A static method expects a class name as the first argument.
  168.           It provides functionality for the class as a whole, not for
  169.           any individual object belonging to the class.  Constructors
  170.           are typically static methods.  Many static methods simply
  171.           ignore their first argument, since they already know what
  172.           package they're in, and don't care what package they were
  173.           invoked via.  (These aren't necessarily the same, since
  174.           static methods follow the inheritance tree just like
  175.           ordinary virtual methods.)  Another typical use for static
  176.           methods is to look up an object by name:
  177.  
  178.               sub find {
  179.                   my ($class, $name) = @_;
  180.                   $objtable{$name};
  181.               }
  182.  
  183.           A virtual method expects an object reference as its first
  184.           argument.  Typically it shifts the first argument into a
  185.           "self" or "this" variable, and then uses that as an ordinary
  186.           reference.
  187.  
  188.  
  189.  
  190.  
  191.  
  192.  
  193.  
  194.  
  195.      Page 3                                          (printed 6/30/95)
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202.      PPPPEEEERRRRLLLLOOOOBBBBJJJJ((((1111))))  UUUUNNNNIIIIXXXX SSSSyyyysssstttteeeemmmm VVVV ((((RRRReeeelllleeeeaaaasssseeee 0000....0000 PPPPaaaattttcccchhhhlllleeeevvvveeeellll 00000000))))  PPPPEEEERRRRLLLLOOOOBBBBJJJJ((((1111))))
  203.  
  204.  
  205.  
  206.               sub display {
  207.                   my $self = shift;
  208.                   my @keys = @_ ? @_ : sort keys %$self;
  209.                   foreach $key (@keys) {
  210.                       print "\t$key => $self->{$key}\n";
  211.                   }
  212.               }
  213.  
  214.  
  215.           MMMMeeeetttthhhhoooodddd IIIInnnnvvvvooooccccaaaattttiiiioooonnnn
  216.  
  217.           There are two ways to invoke a method, one of which you're
  218.           already familiar with, and the other of which will look
  219.           familiar.  Perl 4 already had an "indirect object" syntax
  220.           that you use when you say
  221.  
  222.               print STDERR "help!!!\n";
  223.  
  224.           This same syntax can be used to call either static or
  225.           virtual methods.  We'll use the two methods defined above,
  226.           the static method to lookup an object reference and the
  227.           virtual method to print out its attributes.
  228.  
  229.               $fred = find Critter "Fred";
  230.               display $fred 'Height', 'Weight';
  231.  
  232.           These could be combined into one statement by using a BLOCK
  233.           in the indirect object slot:
  234.  
  235.               display {find Critter "Fred"} 'Height', 'Weight';
  236.  
  237.           For C++ fans, there's also a syntax using -> notation that
  238.           does exactly the same thing.  The parentheses are required
  239.           if there are any arguments.
  240.  
  241.               $fred = Critter->find("Fred");
  242.               $fred->display('Height', 'Weight');
  243.  
  244.           or in one statement,
  245.  
  246.               Critter->find("Fred")->display('Height', 'Weight');
  247.  
  248.           There are times when one syntax is more readable, and times
  249.           when the other syntax is more readable.  The indirect object
  250.           syntax is less cluttered, but it has the same ambiguity as
  251.           ordinary list operators.  Indirect object method calls are
  252.           parsed using the same rule as list operators: "If it looks
  253.           like a function, it is a function".  (Presuming for the
  254.           moment that you think two words in a row can look like a
  255.           function name.  C++ programmers seem to think so with some
  256.           regularity, especially when the first word is "new".)  Thus,
  257.           the parens of
  258.  
  259.  
  260.  
  261.      Page 4                                          (printed 6/30/95)
  262.  
  263.  
  264.  
  265.  
  266.  
  267.  
  268.      PPPPEEEERRRRLLLLOOOOBBBBJJJJ((((1111))))  UUUUNNNNIIIIXXXX SSSSyyyysssstttteeeemmmm VVVV ((((RRRReeeelllleeeeaaaasssseeee 0000....0000 PPPPaaaattttcccchhhhlllleeeevvvveeeellll 00000000))))  PPPPEEEERRRRLLLLOOOOBBBBJJJJ((((1111))))
  269.  
  270.  
  271.  
  272.               new Critter ('Barney', 1.5, 70)
  273.  
  274.           are assumed to surround ALL the arguments of the method
  275.           call, regardless of what comes after.  Saying
  276.  
  277.               new Critter ('Bam' x 2), 1.4, 45
  278.  
  279.           would be equivalent to
  280.  
  281.               Critter->new('Bam' x 2), 1.4, 45
  282.  
  283.           which is unlikely to do what you want.
  284.  
  285.           There are times when you wish to specify which class's
  286.           method to use.  In this case, you can call your method as an
  287.           ordinary subroutine call, being sure to pass the requisite
  288.           first argument explicitly:
  289.  
  290.               $fred =  MyCritter::find("Critter", "Fred");
  291.               MyCritter::display($fred, 'Height', 'Weight');
  292.  
  293.           Note however, that this does not do any inheritance.  If you
  294.           merely wish to specify that Perl should _S_T_A_R_T looking for a
  295.           method in a particular package, use an ordinary method call,
  296.           but qualify the method name with the package like this:
  297.  
  298.               $fred = Critter->MyCritter::find("Fred");
  299.               $fred->MyCritter::display('Height', 'Weight');
  300.  
  301.  
  302.           DDDDeeeessssttttrrrruuuuccccttttoooorrrrssss
  303.  
  304.           When the last reference to an object goes away, the object
  305.           is automatically destroyed.  (This may even be after you
  306.           exit, if you've stored references in global variables.)  If
  307.           you want to capture control just before the object is freed,
  308.           you may define a DESTROY method in your class.  It will
  309.           automatically be called at the appropriate moment, and you
  310.           can do any extra cleanup you need to do.
  311.  
  312.           Perl doesn't do nested destruction for you.  If your
  313.           constructor reblessed a reference from one of your base
  314.           classes, your DESTROY may need to call DESTROY for any base
  315.           classes that need it.  But this only applies to reblessed
  316.           objects--an object reference that is merely _C_O_N_T_A_I_N_E_D in the
  317.           current object will be freed and destroyed automatically
  318.           when the current object is freed.
  319.  
  320.           SSSSuuuummmmmmmmaaaarrrryyyy
  321.  
  322.           That's about all there is to it.  Now you just need to go
  323.           off and buy a book about object-oriented design methodology,
  324.  
  325.  
  326.  
  327.      Page 5                                          (printed 6/30/95)
  328.  
  329.  
  330.  
  331.  
  332.  
  333.  
  334.      PPPPEEEERRRRLLLLOOOOBBBBJJJJ((((1111))))  UUUUNNNNIIIIXXXX SSSSyyyysssstttteeeemmmm VVVV ((((RRRReeeelllleeeeaaaasssseeee 0000....0000 PPPPaaaattttcccchhhhlllleeeevvvveeeellll 00000000))))  PPPPEEEERRRRLLLLOOOOBBBBJJJJ((((1111))))
  335.  
  336.  
  337.  
  338.           and bang your forehead with it for the next six months or
  339.           so.
  340.  
  341.      SSSSEEEEEEEE AAAALLLLSSSSOOOO
  342.           You should also check out the _p_e_r_l_b_o_t manpage for other
  343.           object tricks, traps, and tips.
  344.  
  345.  
  346.  
  347.  
  348.  
  349.  
  350.  
  351.  
  352.  
  353.  
  354.  
  355.  
  356.  
  357.  
  358.  
  359.  
  360.  
  361.  
  362.  
  363.  
  364.  
  365.  
  366.  
  367.  
  368.  
  369.  
  370.  
  371.  
  372.  
  373.  
  374.  
  375.  
  376.  
  377.  
  378.  
  379.  
  380.  
  381.  
  382.  
  383.  
  384.  
  385.  
  386.  
  387.  
  388.  
  389.  
  390.  
  391.  
  392.  
  393.      Page 6                                          (printed 6/30/95)
  394.  
  395.  
  396.  
  397.